
//
//  XSAudioListView.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/10/25.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSAudioListView.h"
#import "XSAudioListViewCollectionViewCell.h"

static CGFloat AudioCellItemHeight = 100;

@interface XSAudioListView ()<UICollectionViewDelegate,UICollectionViewDataSource>

 @property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger selectedIndex;

@end

@implementation XSAudioListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"XSAudioListView" owner:nil options:nil]firstObject];
        self.frame = frame;
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        [self addSubview:self.headerView];
        
        self.selectedIndex = -1;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerNib:[UINib nibWithNibName:@"XSAudioListViewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"XSAudioListViewCollectionViewCell"];
        self.flowLayout.itemSize = CGSizeMake(AudioCellItemHeight, AudioCellItemHeight);
        self.flowLayout.minimumInteritemSpacing = 0;
        self.flowLayout.minimumLineSpacing = 0;
        self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 
    }
    return self;
}


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:5];
        [_dataArray addObject:@"静音"];
        [_dataArray addObject:@"三生三世1"];
        [_dataArray addObject:@"三生三世2"];
        [_dataArray addObject:@"三生三世3"];
        [_dataArray addObject:@"山楂树之恋1"];
        [_dataArray addObject:@"山楂树之恋2"];
        [_dataArray addObject:@"I love you 3000 1"];
        [_dataArray addObject:@"I love you 3000 2"];
        [_dataArray addObject:@"I love you 3000 3"];
 
    }
    return _dataArray;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, 44)];
        
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        btn.frame = CGRectMake(_headerView.width - 100, 0, 80, 40);
//        btn.titleLabel.font = [UIFont systemFontOfSize:15];
//        [btn setTitle:@"确定" forState:UIControlStateNormal];
//        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
//        [_headerView addSubview:btn];
//        btn.hidden = YES;
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(20, 0, 80, 40);
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [cancelBtn setTitle:@"确定" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(cancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:cancelBtn];
        
    }
    return _headerView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XSAudioListViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XSAudioListViewCollectionViewCell" forIndexPath:indexPath];
    cell.label.text = self.dataArray[indexPath.row];
    if (indexPath.row == 0) {
        cell.imageV.image = [UIImage imageNamed:@"jingyin.jpeg"];
    }else{
        cell.imageV.image = [UIImage imageNamed:@"audio-img.jpeg"];
    }
    
    if (self.selectedIndex == indexPath.row) {
        cell.isSelect = YES;
    }else{
        cell.isSelect = NO;
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    self.selectedIndex = indexPath.row;
    [self.collectionView reloadData];
    
    [self btnClicked:nil];
}

- (void)btnClicked:(UIButton *)btn{
    
    if (self.selectedAudioBlock) {
        if (self.selectedIndex > 0) {
            NSString *path  = [[NSBundle mainBundle]pathForResource:self.dataArray[self.selectedIndex] ofType:@"m4r"];
            if (path.length) {
                self.selectedAudioBlock(path);
                 
            }
        }else{
            self.selectedAudioBlock(nil);
        }
        
    }
    
}
- (void)cancelBtnClicked:(UIButton *)btn{
    [self hiddenListView];
}

- (void)hiddenListView{
    [UIView animateWithDuration:0.3 animations:^{
        self.y = kHeight;
    }completion:^(BOOL finished) {
        self.hidden = YES;
    }];
    
}

- (void)showAudioListView{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.y = kHeight - self.height;
    }];
    [self.collectionView reloadData];
}



@end
